import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;


public class chattext extends Thread{
	public String text;
	ServerSocket ss;
	serverGUI gui;
	public chattext (ServerSocket ss_, serverGUI gui_){
		ss = ss_;
		gui = gui_;
	}
	
	public void run(){
		System.out.println("welcome to service chattext on " + ss.getLocalPort());
		Socket socket;
		try {
			socket = ss.accept();
			DataInputStream dis;
			dis = new DataInputStream(socket.getInputStream());
			while(true){
				System.out.println("your text: " + dis.readUTF());
				gui.postext2(dis.readUTF());
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		serverGUI abc = new serverGUI();
		try {
			ServerSocket ss = new ServerSocket(8889);
			chattext instance = new chattext(ss, abc);
			instance.start();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}
}

class serverGUI extends JFrame {
	private JTextArea textarea1 ;
	private JTextArea textarea2 ;
	private JButton submit ;
	public serverGUI (){
		setSize(600,300);
		setLocation(300, 200);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setResizable(false);
		
		JPanel panel = panel1();
		getContentPane().add(panel);
		
		setVisible(true);
	}
	
	private JPanel panel1 (){
		JPanel panel = new JPanel();
		panel.setLayout(new BorderLayout());
		
		//GridBagConstraints gbc = new GridBagConstraints();
		
		textarea1 = new JTextArea("1");
		JScrollPane scr1 = new JScrollPane(textarea1);
		panel.add(scr1,BorderLayout.CENTER);
		
		JPanel panel2 = new JPanel();
		
		textarea2 = new JTextArea("2",3,47);
		JScrollPane scr2 = new JScrollPane(textarea2);
		panel2.add(scr2);
		
		submit = new JButton("Send");
		panel2.add(submit);
		
		submit.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				String inputtext = textarea2.getText();
					posttext(inputtext);
				}
		});
		
		textarea2.addKeyListener(new KeyListener() {
			
			@Override
			public void keyTyped(KeyEvent arg0) {
				// TODO Auto-generated method stub
				if(arg0.getKeyCode() == KeyEvent.VK_ENTER){
					System.out.println(arg0.getKeyCode());
					String inputtext = textarea2.getText();
					posttext(inputtext);
				}
			}
			public void keyReleased(KeyEvent arg0) {}
			public void keyPressed(KeyEvent arg0) {}
		});
		
		panel.add(panel2, BorderLayout.SOUTH);
		
		return panel;
	}
	
	private void posttext (String input){
		if(!input.equals(null) && !input.equals(""))
			textarea1.append("you: " + input + "\r\n");
		textarea2.setText("");
	}
	
	public void postext2 (String input){
		if(!input.equals(null) && !input.equals(""))
			textarea1.append("your friend: " + input + "\r\n");
	}
}
